﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using KPIS.GERP.ESS.APP.GerpAuthenService;
using KPIS.GERP.ESS.APP.SysApprovalService;
using Telerik.Web.UI;
using KPIS.GERP.ESS.APP.App_UI.Commons;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.DAL;
using KPIS.GERP.ESS.MODEL;
using Exception = System.Exception;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class TaskListsPopUp : System.Web.UI.Page
    {
        private Main main = new Main();
        private B_MessageSend send = new B_MessageSend();
        private B_MessageReply reply = new B_MessageReply();
        private D_HrmJobsites jobsite = new D_HrmJobsites();
        private FIM_MoneyRequests.FIM_MoneyRequests mr = new FIM_MoneyRequests.FIM_MoneyRequests();
        private FIM_MoneyRequestDtls.FIM_MoneyRequestDtls mrd = new FIM_MoneyRequestDtls.FIM_MoneyRequestDtls();
        private FIM_Petitions.FIM_Petitions pet = new FIM_Petitions.FIM_Petitions();
        private FIM_Payments.FIM_Payments pay = new FIM_Payments.FIM_Payments();
        private GerpAuthenService.GerpAuthenService authen = new GerpAuthenService.GerpAuthenService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoginViaTokenKeyThenCheck(Request.QueryString["tokenKey"]);
                ltrWodList.Text = GetWordListAsHtmlString();
                SetControl("ViewAll");
                LoadDDLFilterJobSiteSeq();
                cmdViewRequest_Click(null, null);
            }
        }

        protected void LoadRadGridRequest(string type="All")
        {
            string strEmpSeq = (Session["EmpSeq"] != null ? Session["EmpSeq"].ToString() : Request.QueryString["Seq"]);

            var wodList = GetWod();

            List<M_MessageSend> datasource = send.GetFullWithApproveCommandStatus(strEmpSeq, null, null, ddlFilterJobsiteSeq.SelectedValue, ddlRefType.SelectedValue, null, wodList);

            datasource = cmdMultiSelectStart.Visible || type=="All" ? 
                datasource : 
                datasource.Where(o => o.privilege != null && o.privilege.is_approvable == true).ToList();

            RadGridRequest.DataSource = datasource;
            RadGridRequest.DataBind();
        }

        protected void LoadRadGridReply()
        {
            string strEmpSeq = (Session["EmpSeq"] != null ? Session["EmpSeq"].ToString() : Request.QueryString["Seq"]);

            RadGridReply.DataSource = reply.GetFull(strEmpSeq, null, null);
            RadGridReply.DataBind();
        }

        protected void LoadRadGridSent()
        {
            string strEmpSeq = (Session["EmpSeq"] != null ? Session["EmpSeq"].ToString() : Request.QueryString["Seq"]);

            RadGridSent.DataSource = send.GetFull(null, strEmpSeq, null, ddlFilterJobsiteSeq.SelectedValue, ddlRefType.SelectedValue, null);
            RadGridSent.DataBind();
        }

        protected void LoadDDLFilterJobSiteSeq()
        {
            ddlFilterJobsiteSeq.DataSource = jobsite.GetList();
            ddlFilterJobsiteSeq.DataBind();

            main.InsertEmptyDDLAll(ddlFilterJobsiteSeq);
            ddlFilterJobsiteSeq.SelectedIndex = 0;
        }

        protected string GetStatusPicture(string strMessageStatus)
        {
            string strUrl = "";

            switch (strMessageStatus)
            {
                case "0":
                    strUrl = "../Images/Buttons/email.png";
                    break;
                case "1":
                    strUrl = "../Images/Buttons/email_open_image.png";
                    break;
                case "2":
                    strUrl = "../Images/Buttons/ReplyedMail.png";
                    break;
            }

            return strUrl;
        }

        protected string GetStatusToolTip(string strMessageStatus)
        {
            string strText = "";

            switch (strMessageStatus)
            {
                case "0":
                    strText = "ยังไม่ได้เปิดอ่าน";
                    break;
                case "1":
                    strText = "เปิดอ่านแล้ว";
                    break;
                case "2":
                    strText = "ตอบกลับแล้ว";
                    break;
            }

            return strText;
        }

        public void ViewRequest(string type="All")
        {
            LoadRadGridRequest(type);

            PanelRadGridRequest.Visible = true;
            PanelRadGridReply.Visible = false;
            PanelRadGridSent.Visible = false;
            PanelRequest.Visible = false;
            PanelReply.Visible = false;
            PanelRequest.Visible = false;
            PanelACRequest.Visible = false;
            PanelRequestAbsence.Visible = false;
            PanelRequestTraining.Visible = false;
            PanelMailActivity.Visible = false;
            PanelBgmRequest.Visible = false;
            PanelBgmProjectOperation.Visible = false;
            PanelBgmProjectOperationEdit.Visible = false;
            PanelIcmPrePurchaseRequest.Visible = false;
            PanelPetitions.Visible = false;
            PanelPayments.Visible = false;
            PanelTask.Visible = false;
            PanelBgmProjectOperationEdit.Visible = false;
            PanelReturn.Visible = false;
            PanelFimMoneyReturns.Visible = false;
        }

        public void ViewReply()
        {
            LoadRadGridReply();

            PanelRadGridRequest.Visible = false;
            PanelRadGridReply.Visible = true;
            PanelRadGridSent.Visible = false;
            PanelRequest.Visible = false;
            PanelReply.Visible = false;
            PanelRequest.Visible = false;
            PanelACRequest.Visible = false;
            PanelRequestAbsence.Visible = false;
            PanelRequestTraining.Visible = false;
            PanelMailActivity.Visible = false;
            PanelBgmRequest.Visible = false;
            PanelBgmProjectOperation.Visible = false;
            PanelBgmProjectOperationEdit.Visible = false;
            PanelIcmPrePurchaseRequest.Visible = false;
            PanelPetitions.Visible = false;
            PanelPayments.Visible = false;
            PanelTask.Visible = false;
            PanelBgmProjectOperationEdit.Visible = false;
            PanelReturn.Visible = false;
            PanelFimMoneyReturns.Visible = false;
        }

        public void ViewSent()
        {
            LoadRadGridSent();

            PanelRadGridRequest.Visible = false;
            PanelRadGridReply.Visible = false;
            PanelRadGridSent.Visible = true;
            PanelRequest.Visible = false;
            PanelReply.Visible = false;
            PanelRequest.Visible = false;
            PanelACRequest.Visible = false;
            PanelRequestAbsence.Visible = false;
            PanelRequestTraining.Visible = false;
            PanelMailActivity.Visible = false;
            PanelBgmRequest.Visible = false;
            PanelBgmProjectOperation.Visible = false;
            PanelBgmProjectOperationEdit.Visible = false;
            PanelIcmPrePurchaseRequest.Visible = false;
            PanelPetitions.Visible = false;
            PanelPayments.Visible = false;
            PanelTask.Visible = false;
            PanelBgmProjectOperationEdit.Visible = false;
            PanelReturn.Visible = false;
            PanelFimMoneyReturns.Visible = false;
        }

        protected void SetControl(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                    //cmdAddNew1.Visible = true;
                    //cmdEdit1.Visible = true;
                    //cmdDelete1.Visible = true;
                    //cmdInsert1.Visible = false;
                    //cmdUpdate1.Visible = false;
                    //cmdCancel.Visible = false;

                    lblModeName.Text = "แสดงรายการ";
                    lblMode.Text = "V";
                    //cmdViewAll_Click(null, null);
                    break;
                case "Info":
                    //cmdAddNew1.Visible = false;
                    //cmdEdit1.Visible = false;
                    //cmdDelete1.Visible = false;
                    //cmdInsert1.Visible = true;
                    //cmdUpdate1.Visible = false;
                    //cmdCancel.Visible = false;

                    lblModeName.Text = "รายละเอียด";
                    lblMode.Text = "A";
                    //cmdProgress1_Click(null, null);
                    break;
                    //case "AddNew":
                    //    cmdAddNew1.Visible = false;
                    //    cmdEdit1.Visible = false;
                    //    cmdDelete1.Visible = false;
                    //    cmdInsert1.Visible = true;
                    //    cmdUpdate1.Visible = false;
                    //    cmdCancel.Visible = false;

                    //    lblModeName.Text = "เพิ่มรายการใหม่";
                    //    lblMode.Text = "A";
                    //    //cmdProgress1_Click(null, null);
                    //    break;
                    //case "Edit":
                    //    cmdAddNew1.Visible = false;
                    //    cmdEdit1.Visible = false;
                    //    cmdDelete1.Visible = false;
                    //    cmdInsert1.Visible = false;
                    //    cmdUpdate1.Visible = true;
                    //    cmdCancel.Visible = true;

                    //    lblModeName.Text = "แก้ไขรายการ";
                    //    lblMode.Text = "E";
                    //    //cmdSummary_Click(null, null);
                    //    break;
                case "NoControl":
                    //cmdAddNew1.Visible = false;
                    //cmdEdit1.Visible = false;
                    //cmdDelete1.Visible = false;
                    //cmdInsert1.Visible = false;
                    //cmdUpdate1.Visible = false;
                    //cmdCancel.Visible = false;

                    lblModeName.Text = "แสดงรายการ";
                    lblMode.Text = "N";
                    break;
            }
        }

        //======================= PanelMenu ===============================

        public void cmdViewRequest_Click(object sender, EventArgs e)
        {
            li1.Attributes["class"] = "Selected";
            li2.Attributes["class"] = "";
            li3.Attributes["class"] = "";

            cmdMultiSelectStart.Visible = true;
            cmdMultiSelectStop.Visible = false;
            ViewRequest();
        }

        public void cmdViewReply_Click(object sender, EventArgs e)
        {
            li1.Attributes["class"] = "";
            li2.Attributes["class"] = "Selected";
            li3.Attributes["class"] = "";

            cmdMultiSelectStart.Visible = false;
            cmdMultiSelectStop.Visible = false;
            ViewReply();
        }

        protected void cmdViewSent_Click(object sender, EventArgs e)
        {
            li1.Attributes["class"] = "";
            li2.Attributes["class"] = "";
            li3.Attributes["class"] = "Selected";

            cmdMultiSelectStart.Visible = false;
            cmdMultiSelectStop.Visible = false;
            ViewSent();
        }

        //======================= PanelRadGridRequest ===============================
        
        protected void RadGridRequest_ItemCommand(object source, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
                {
                    LoadRadGridRequest();
                }
                else if (e.CommandName == "RowClick" && e.Item is GridDataItem && cmdMultiSelectStart.Visible)
                { 
                    e.Item.Selected = true; 
                    string strSendSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["send_seq"].Text);
                    string strRefSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_seq"].Text);
                    string strRefType = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_type"].Text);
                    string strFromEmpSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["from_emp_seq"].Text);
                    string strToEmpSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["to_emp_seq"].Text);
                    string strMessageStatus = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["message_status"].Text);
                    string strFromSendSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["from_send_seq"].Text);
                    string strSendDate = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["send_date"].Text);
                    string strRefNo = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_no"].Text);
                    string strSendText = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["send_text"].Text);
                    string strFromUserName = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["from_user_name"].Text);
                    string strApprovePathSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["approve_path_seq"].Text);
                     
                    bool bAbsenceRequest = strRefType == "hrm_ps_absences";
                    PanelRequestAbsence.Visible = bAbsenceRequest;

                    //if (strMessageStatus == "0")
                    //{
                    //    M_MessageSend messageInfo = new M_MessageSend();
                    //    messageInfo.send_seq = Convert.ToInt32(strSendSeq);
                    //    messageInfo.message_status = "1";
                    //    messageInfo.updated_by = SsMng.EmpSeqI;
                    //    messageInfo.updated_when = DateTime.Now;
                    //    send.UpdateMessageStatus(messageInfo);
                    //}

                    switch (strRefType)
                    {
                        case "fim_money_requests":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = true;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            FIM_MoneyRequests.M_FIM_MoneyRequests info = mr.GetInfo(strRefSeq);
                            Request1.LoadData(strSendSeq, info, true, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            TaskMessage.UnloadTaskMessage();

                            break;
                        case "bgm_request":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = true;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            Mail_BgmRequestTemp1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "bgm_project_operation":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = true;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            Mail_BgmProjectOperation1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "fim_petitions":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = true;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            FIM_Petitions.M_FIM_Petitions petInfo = pet.GetInfo(strRefSeq);
                            Mail_Petition1.LoadData(strSendSeq, petInfo, true, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "fim_payments":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = true;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            FIM_Payments.M_FIM_Payments payInfo = pay.GetInfo(strRefSeq);
                            Mail_Payment1.LoadData(strSendSeq, strRefSeq, true, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            TaskMessage.UnloadTaskMessage();
                            break;
                            // =========  ใส่ Ref_Type ========
                        case "icm_pre_purchase_request": //แจ้งความประสงค์ขอซื้อจ้าง
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = true;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            Mail_IcmPrePurchaseRequest1.LoadData(Int32.Parse(strRefSeq), strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus,
                                true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                            // =========  HR ================
                        case "hrm_ac_activities": //แจ้งเวียนกิจกรรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = true;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            MailActivity1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "hrm_ac_activity_requests": // อนุมัติขอร่วมกิจกรรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = true;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            MailActivity1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "hrm_ps_absences": //ขอลา
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = true;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            RequestAbsence1.LoadDataApproved(Int32.Parse(strRefSeq), true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "hrm_training_courses": //แจ้งฝึกอบรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = true;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            RequestAbsence1.LoadDataApproved(Int32.Parse(strRefSeq), true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "hrm_training_requests": //ขออนุมัติเข้ารับการฝึกอบรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = true;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            MailRequestTraining1.LoadMessageSend(strSendSeq);
                            MailRequestTraining1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "sys_task": // ขออนุมัติการลา, แจ้งความประสงค์ ฯลฯ ดูจากประเภทได้ใน table sys_approve_process
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = true;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false; 
                            TaskMessage.LoadTaskMessage(Int32.Parse(strRefSeq), strSendSeq);
                            break;
                        case "bgm_project_operation_edit": // bgm_project_operation_edit
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = true;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = false;

                            Mail_BgmProjectOperationEdit1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "return": // return
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = true;
                            PanelFimMoneyReturns.Visible = false;

                            Mail_Return1.LoadData(strSendSeq);
                            TaskMessage.UnloadTaskMessage();
                            break;
                        case "fim_money_returns": // fim_money_returns
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = false;
                            PanelBgmProjectOperationEdit.Visible = false;
                            PanelReturn.Visible = false;
                            PanelFimMoneyReturns.Visible = true;

                            Mail_FimMoneyReturns1.LoadData(strSendSeq, strRefSeq);
                            TaskMessage.UnloadTaskMessage();
                            break;
                    }
                    SetVisibleCmdMultiActionSelectLink();
                }
            }
            catch (Exception ex)
            {

                main.MessageAlert(Page, main.EncodeJsString(ex.Message));
            }
        }



        //======================= PanelRadGridReply ===============================

        protected void RadGridReply_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGridReply();
            }
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                string strReplySeq = main.WebString(RadGridReply.Items[e.Item.ItemIndex]["reply_seq"].Text);
                string strRefSeq = null;
                try
                {
                    strRefSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_seq"].Text);
                }
                catch (Exception ex)
                {
                    string message = "Error found on TaskListPopup.aspx.cs - RadGridReply_ItemCommand(source, e): Error message is " + ex.Message;
                    main.MessageAlert(Page, main.EncodeJsString(message));
                    return;
                }
                string strRefType = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_type"].Text);

                //By KHIM ********************************
                bool bAbsenceRequest = strRefType == "hrm_ps_absences";
                PanelRequestAbsence.Visible = bAbsenceRequest;
                //****************************************

                switch (strRefType)
                {
                    case "hrm_ps_absences":
                        PanelRadGridRequest.Visible = false;
                        PanelRadGridReply.Visible = false;
                        //PanelRadGridTaskList.Visible = false;
                        PanelRequest.Visible = false;
                        PanelReply.Visible = false;
                        PanelACRequest.Visible = false;
                        RequestAbsence1.Visible = true;
                        RequestAbsence1.LoadDataApproved(Int32.Parse(strRefSeq), true);
                        break;
                    default:
                        PanelRadGridRequest.Visible = false;
                        PanelRadGridReply.Visible = false;
                        //PanelRadGridTaskList.Visible = false;
                        PanelRequest.Visible = false;
                        PanelReply.Visible = true;
                        Reply1.Visible = true;
                        Reply1.LoadData(strReplySeq);
                        break;
                }
            }
        }

        //======================= PanelRadGridSent ===============================

        protected void RadGridSent_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGridSent();
            }
            else if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                string strRefSeq = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["ref_seq"].Text);
                string strRefType = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["ref_type"].Text);
                try
                {
                    e.Item.Selected = true;
                    string strSendSeq = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["send_seq"].Text);
                    string strFromEmpSeq = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["from_emp_seq"].Text);
                    string strToEmpSeq = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["to_emp_seq"].Text);
                    string strMessageStatus = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["message_status"].Text);
                    string strFromSendSeq = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["from_send_seq"].Text);
                    string strSendDate = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["send_date"].Text);
                    string strRefNo = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["ref_no"].Text);
                    string strSendText = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["send_text"].Text);
                    string strFromUserName = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["from_user_name"].Text);
                    string strApprovePathSeq = main.WebString(RadGridSent.Items[e.Item.ItemIndex]["approve_path_seq"].Text);

                    bool bAbsenceRequest = strRefType == "hrm_ps_absences";
                    PanelRequestAbsence.Visible = bAbsenceRequest;
                    PanelRadGridSent.Visible = false;
                    PanelRadGridReply.Visible = false;
                    PanelRadGridSent.Visible = false;

                    switch (strRefType)
                    {
                        case "fim_money_requests":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = true;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            FIM_MoneyRequests.M_FIM_MoneyRequests info = mr.GetInfo(strRefSeq);
                            Request1.LoadData(strSendSeq, info, false, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            break;
                        case "bgm_request":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = true;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            Mail_BgmRequestTemp1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, false);
                            break;
                        case "bgm_project_operation":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = true;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            Mail_BgmProjectOperation1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, false);
                            break;
                        case "fim_petitions":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = true;
                            PanelPayments.Visible = false;

                            FIM_Petitions.M_FIM_Petitions petInfo = pet.GetInfo(strRefSeq);
                            Mail_Petition1.LoadData(strSendSeq, petInfo, false, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            break;
                        case "fim_payments":
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = true;

                            FIM_Payments.M_FIM_Payments payInfo = pay.GetInfo(strRefSeq);
                            Mail_Payment1.LoadData(strSendSeq, strRefSeq, false, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            break;
                            // =========  ใส่ Ref_Type ========
                        case "icm_pre_purchase_request": //แจ้งความประสงค์ขอซื้อจ้าง
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = true;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            Mail_IcmPrePurchaseRequest1.LoadData(Int32.Parse(strRefSeq), strFromEmpSeq, strApprovePathSeq, strSendSeq,
                                strMessageStatus, false);
                            break;
                            // =========  HR ================
                        case "hrm_ac_activities": //แจ้งเวียนกิจกรรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = true;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            MailActivity1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), false);
                            break;
                        case "hrm_ac_activity_requests": // อนุมัติขอร่วมกิจกรรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = true;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            MailActivity1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), false);
                            break;
                        case "hrm_ps_absences": //ขอลา
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = true;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;

                            RequestAbsence1.LoadDataApproved(Int32.Parse(strRefSeq), false);
                            break;
                        case "hrm_training_courses": //แจ้งฝึกอบรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = true;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            RequestAbsence1.LoadDataApproved(Int32.Parse(strRefSeq), false);
                            break;
                        case "hrm_training_requests": //ขออนุมัติเข้ารับการฝึกอบรม
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = true;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;

                            MailRequestTraining1.LoadMessageSend(strSendSeq);
                            MailRequestTraining1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), false);
                            break;
                        case "sys_task": // ขออนุมัติการลา, แจ้งความประสงค์ ฯลฯ ดูจากประเภทได้ใน table sys_approve_process
                            PanelRadGridRequest.Visible = false;
                            PanelRadGridReply.Visible = false;
                            PanelRadGridSent.Visible = false;
                            PanelRequest.Visible = false;
                            PanelReply.Visible = false;
                            PanelRequest.Visible = false;
                            PanelACRequest.Visible = false;
                            PanelRequestAbsence.Visible = false;
                            PanelRequestTraining.Visible = false;
                            PanelMailActivity.Visible = false;
                            PanelBgmRequest.Visible = false;
                            PanelBgmProjectOperation.Visible = false;
                            PanelIcmPrePurchaseRequest.Visible = false;
                            PanelPetitions.Visible = false;
                            PanelPayments.Visible = false;
                            PanelTask.Visible = true; 
                            TaskMessage.LoadTaskMessage(Int32.Parse(strRefSeq),  strSendSeq);
                            break;
                    }
                }
                catch (Exception ex)
                {
                    string message = main.EncodeJsString(string.Format("Can not find id [{0}] of type [{1}], internal error message = ",
                        strRefSeq, strRefType, ex.Message));
                    RadAjaxManager.GetCurrent(Page).Alert(message); 
                }
            }
        }

        //============================================================================

        protected void cmdBack_Click(object sender, EventArgs e)
        {
            if (li1.Attributes["class"] == "Selected")
            {
                ViewRequest("All");
            }
            else if (li2.Attributes["class"] == "Selected")
            {
                ViewReply();
            }
            else if (li3.Attributes["class"] == "Selected")
            {
                ViewSent();
            }
            SetVisibleCmdMultiActionSelectLink("StopMultiAction");
        }

        private void LoadGrid(string param = "All")
        {
            if (PanelRadGridRequest.Visible)
            {
                LoadRadGridRequest(param);
            }
            else if (PanelRadGridReply.Visible)
            {
                LoadRadGridReply();
            }
        }

        protected void ddlFilterJobsiteSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadGrid();
        }

        protected void ddlRefType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadGrid();
        }

        protected void RadGridRequest_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
        {
            CurrentPageSize = e.NewPageSize;
            LoadGrid();
        }
        private int? CurrentPageSize { get; set; }
        protected void cmd_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.ToString().EndsWith("MultiAction"))
            {
                SetVisibleCmdMultiActionSelectLink(e.CommandArgument.ToString());
                LoadGrid(e.CommandArgument.ToString().StartsWith("Start") ? "Some" : "All");
            }
            else if (e.CommandArgument.ToString() == "ApproveConfirmPopup")
            {
                var item = (from GridDataItem gi in RadGridRequest.SelectedItems
                            select  
                                Convert.ToInt32(gi.KeyValues.Split('"')[1]) 
                                /* ตัวอย่างคือ string ที่ดึงจาก KeyValues คือ
                                 * {send_seq:"16570"} */).ToList();
                var sendSeq = (from i in item select Convert.ToInt32(i)).ToList();
                var sendSeqStr = string.Join(",", (from i in item select Convert.ToString(i)).ToArray());
                string strEmpSeq = (Session["EmpSeq"] != null ? Session["EmpSeq"].ToString() : Request.QueryString["Seq"]);
                List<M_MessageSend> allSelectedMessageSends = 
                    send.GetFullWithApproveCommandStatus(strEmpSeq, null, null, null, null, null, null)
                    .Where(o => sendSeq.Contains(o.send_seq??0)).ToList();
                string errorMessage = "";
                int success = 0;
                foreach (var ms in allSelectedMessageSends)
                {
                    string strRefSeq = ms.ref_seq.ToString();
                    string strFromEmpSeq = Convert.ToString(ms.from_emp_seq);
                    string strApprovePathSeq = Convert.ToString(ms.approve_path_seq);
                    string strSendSeq = ms.send_seq.ToString();
                    string strMessageStatus = Convert.ToString(ms.message_status);
                    string eachErrorMessage = "";
                    switch (ms.ref_type)
                    {
                        case "fim_money_requests":
                            FIM_MoneyRequests.M_FIM_MoneyRequests info = mr.GetInfo(strRefSeq);
                            Request1.LoadData(strSendSeq, info, true, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            eachErrorMessage = Request1.Approve(txtRemark.Text);
                            break;
                        case "bgm_request":
                            Mail_BgmRequestTemp1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            eachErrorMessage = Mail_BgmRequestTemp1.Approve(txtRemark.Text);
                            break;
                        case "bgm_project_operation":
                            Mail_BgmProjectOperation1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            eachErrorMessage = Mail_BgmProjectOperation1.Approve(txtRemark.Text);
                            break;
                        case "fim_petitions":
                            FIM_Petitions.M_FIM_Petitions petInfo = pet.GetInfo(strRefSeq);
                            Mail_Petition1.LoadData(strSendSeq, petInfo, true, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            eachErrorMessage = Mail_Petition1.Approve(txtRemark.Text);
                            break;
                        case "fim_payments":
                            FIM_Payments.M_FIM_Payments payInfo = pay.GetInfo(strRefSeq);
                            Mail_Payment1.LoadData(strSendSeq, strRefSeq, true, strApprovePathSeq, strMessageStatus, strFromEmpSeq);
                            eachErrorMessage = Mail_Payment1.Approve(txtRemark.Text);
                            break;
                        // =========  ใส่ Ref_Type ========
                        case "icm_pre_purchase_request": //แจ้งความประสงค์ขอซื้อจ้าง
                            Mail_IcmPrePurchaseRequest1.LoadData(Int32.Parse(strRefSeq), strFromEmpSeq,
                                strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            eachErrorMessage = Mail_IcmPrePurchaseRequest1.Approve(txtRemark.Text);
                            break;
                        // =========  HR ================
                        case "hrm_ac_activities": //แจ้งเวียนกิจกรรม
                            MailActivity1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), true);
                            eachErrorMessage = MailActivity1.Approve();
                            break;
                        case "hrm_ac_activity_requests": // อนุมัติขอร่วมกิจกรรม
                            MailActivity1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), true);
                            eachErrorMessage = MailActivity1.Approve();
                            break;
                        case "hrm_training_requests": //ขออนุมัติเข้ารับการฝึกอบรม
                            MailRequestTraining1.LoadMessageSend(strSendSeq);
                            MailRequestTraining1.LoadDetail(Int32.Parse(strSendSeq), Int32.Parse(strRefSeq), true);
                            eachErrorMessage = MailRequestTraining1.Approve();
                            break;
                        case "bgm_project_operation_edit": // bgm_project_operation_edit
                            Mail_BgmProjectOperationEdit1.LoadData(strRefSeq, strFromEmpSeq, strApprovePathSeq, strSendSeq, strMessageStatus, true);
                            eachErrorMessage = Mail_BgmProjectOperationEdit1.Approve(txtRemark.Text);
                            break;
                        //case "hrm_ps_absences": //ขอลา
                        //    RequestAbsence1.LoadDataApproved(Int32.Parse(strRefSeq), true);
                        //    break;
                        case "sys_task": // ขออนุมัติการลา, แจ้งความประสงค์ ฯลฯ ดูจากประเภทได้ใน table sys_approve_process
                            TaskMessage.LoadTaskMessage(Int32.Parse(strRefSeq));
                            var sysapprovalService = new SysApprovalService.SysApprovalService();
                            ActionTaskVM action = new ActionTaskVM();
                            action.ByEmpId = SsMng.EmpSeqI;
                            action.ByEmpIdSpecified = true;
                            action.CommandAction = CommandAction.Approve;
                            action.CommandActionSpecified = true;
                            action.NewTitle = ms.send_text;
                            action.TaskHistoryId = ms.ref_seq;
                            action.TaskHistoryIdSpecified = true;
                            action.Remarks = txtRemark.Text;
                            action = sysapprovalService.ActionTask(action);
                            eachErrorMessage = action.CommandActionErrorResult;
                            break;
                    }
                    if (!string.IsNullOrEmpty(eachErrorMessage))
                    {
                        errorMessage += "[" + ms.ref_no + "]: " + eachErrorMessage + " " ;
                    }
                    else
                    {
                        success++;
                    }
                }

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    errorMessage = (errorMessage ?? "").Replace('\'', ' ').Replace('"', ' ');
                    errorMessage = "Success " + success + " items from " + (allSelectedMessageSends.Count) + "items\r\n" + errorMessage;
                    errorMessage = main.EncodeJsString(errorMessage);
                    main.MessageAlert(Page, errorMessage);
                }
                TaskMessage.UnloadTaskMessage();
                LoadRadGridRequest();

                //  RadWindowRequestApproveConfirm.VisibleOnPageLoad = true;
                //var ds = send.GetFullWithApproveCommandStatus(SsMng.EmpSeq, null, null,
                //    ddlFilterJobsiteSeq.SelectedValue,
                //    ddlRefType.SelectedValue, null, GetWod()).Where(o => item.Contains(o.send_seq.Value));

                // RadGridRequestComfirm.DataSource = ds;
                // RadGridRequestComfirm.DataBind();

            }
        }

        private void SetVisibleCmdMultiActionSelectLink(string command = "StopMultiAction")
        { 
            cmdMultiSelectStart.Visible = command == "StopMultiAction" && RadGridRequest.Visible;
            cmdMultiSelectStop.Visible = command == "StartMultiAction" && RadGridRequest.Visible;
            cmdApproveConfirmPopup.Visible = command == "StartMultiAction" && RadGridRequest.Visible;
            tbReasons.Visible = command == "StartMultiAction" && RadGridRequest.Visible;
            RadGridRequest.MasterTableView.GetColumn("action_select").Visible = command == "StartMultiAction";
            RadGridRequest.MasterTableView.GetColumn("is_approvable").Visible = false; //command == "StartMultiAction";
            RadGridRequest.AllowMultiRowSelection = command == "StartMultiAction";
            RadGridRequest.ClientSettings.EnablePostBackOnRowClick = command != "StartMultiAction";
            RadGridRequest.ClientSettings.Selecting.EnableDragToSelectRows = command == "StartMultiAction";
            RadGridRequest.ClientSettings.Selecting.AllowRowSelect = true; //= command == "StartMultiAction";
        }


        private List<MpPositionWorkOnDutyVM> GetWod()
        {
            var result = new List<MODEL.MpPositionWorkOnDutyVM>();
            B_HrmEmployees eBll = new B_HrmEmployees();
            var dataTable = eBll.ListEmployeeByType("", "");
            var employees = from DataRow dr in dataTable.Rows select dr;
            using (var wods = new WorkOnDutyService.WorkOnDutyService())
            {
                var list = wods.Select(SsMng.EmpSeqI, true).ToList();

                foreach (var wod in list)
                {
                    var toemp = (from e in employees
                        where Convert.ToString(e["t_pos_seq"]) == Convert.ToString(wod.ToPosSeq)
                        select e).FirstOrDefault();

                    var toempfullname = toemp == null ? "" : "" + toemp["full_name"];
                    var toposname = toemp == null ? "" : "" + toemp["posName"];
                    var toposno = toemp == null ? "" : "" + toemp["ref_no"];
                    var byempfullname = wod.ByEmployee == null ? "" : wod.ByEmployee.FirstName + " " + wod.ByEmployee.LastName;

                    var result1 = new MpPositionWorkOnDutyVM();
                    result1.WodId = wod.WodId;
                    result1.Code = wod.Code;
                    result1.ToPosSeq = wod.ToPosSeq.Value;
                    result1.ToEmpSeq = toemp != null ? Convert.ToInt32(toemp["sequence"]) : 0;
                    result1.ByEmpSeq = wod.ByEmpSeq.Value;
                    result1.StartDate = wod.StartDate;
                    result1.EndDate = wod.EndDate;
                    result1.SeeOldMessage = wod.SeeOldMessage;
                    result1.Description = wod.Description;
                    result1.RecordStatus = wod.RecordStatus;
                    result1.ToEmployeeName = toempfullname;
                    result1.ToPosNo = toposno;
                    result1.ToPosName = toposname;
                    result1.ByEmployeeName = byempfullname;
                     
                    result.Add(result1);
                }
            }
            return result;
        }

        private string GetWordListAsHtmlString()
        {
            var wods = GetWod();
            if (wods.Count > 0)
            {
                string empList = string.Join(", ", (from wod in wods
                                                    select string.Format("[{3}] {0} {1} - {2}", wod.ToPosName,
                                                        wod.StartDate.ToString("d/M/yy HH:mm"), wod.EndDate.ToString("d/M/yy HH:mm"), wod.ToPosNo)).ToArray());

                return "<span style='color:yellow; font-weight:bold; cursor:pointer;' onclick='javascript: popup3();'>รักษาการแทน " + empList + "</span>&nbsp;";
            }
            return string.Empty;
        }

        private void LoginViaTokenKeyThenCheck(string tokenKey)
        {
            OTP otp;
            if (!string.IsNullOrEmpty(tokenKey) && (otp = authen.GetOTP(tokenKey)) != null && otp.EmpId != 0)
                SsMng.SetLoginSession(otp.UserId.ToString(), otp.EmpId.ToString(), otp.EmployeeFullName,
                    Convert.ToString(otp.WorkingJobSiteSeq), "480", tokenKey,
                    null, otp.WorkingJobSiteName, Convert.ToString(otp.WorkAreaId));

            // Check if logged-in (1. from ess or 2. from token key)
            if (!SsMng.IsLogin())
            {
                Response.Clear();
                Response.Write("Error: You must log-in");
                Response.End();
            }
            else
            {
                lblPage.ToolTip = SsMng.UserName;
            }
        }

        protected void cmdShowAll_Click(object sender, EventArgs e)
        {

        }

        //protected void BorrowRequest1_ItemDataBound(object sender, GridItemEventArgs e)
        //{
        //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        //    {
        //        Control cmd = e.Item.FindControl("cmdShowApprovePath");
        //        M_MessageSend currentRow = (M_MessageSend)e.Item.DataItem;
        //        //Add the image id to the tooltip manager
        //        this.RadToolTipManager1.TargetControls.Add(cmd.ClientID, currentRow.send_seq.ToString(), true);
        //    }
        //}

        //protected void RadGridSent_ItemDataBound(object sender, GridItemEventArgs e)
        //{
        //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        //    {
        //        Control cmd = e.Item.FindControl("cmdShowApprovePath");
        //        M_MessageSend currentRow = (M_MessageSend)e.Item.DataItem;
        //        //Add the image id to the tooltip manager
        //        this.RadToolTipManager1.TargetControls.Add(cmd.ClientID, currentRow.send_seq.ToString(), true);
        //    }
        //}

        //protected void RadGridReply_ItemDataBound(object sender, GridItemEventArgs e)
        //{
        //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        //    {
        //        Control cmd = e.Item.FindControl("cmdShowApprovePath");
        //        M_MessageSend currentRow = (M_MessageSend)e.Item.DataItem;
        //        //Add the image id to the tooltip manager
        //        this.RadToolTipManager1.TargetControls.Add(cmd.ClientID, currentRow.from_send_seq.ToString(), true);
        //    }
        //}

        //protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        //{
        //    this.UpdateToolTip(args.Value, args.UpdatePanel);
        //}

        //private void UpdateToolTip(string elementID, UpdatePanel panel)
        //{
        //    Control ctrl = Page.LoadControl("../UserControls/ApprovePath.ascx");
        //    panel.ContentTemplateContainer.Controls.Add(ctrl);
        //    UserControls.ApprovePath details = (UserControls.ApprovePath)ctrl;
        //    details.LoadTreeList(elementID);
        //}
    }
}